﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace SnapLib
{
   public class DC:IDisposable
   {
      internal IntPtr m_handle;
      internal ForeignWindow m_parent;
      enum ReleaseMethod { Release, Delete };
      ReleaseMethod m_ReleaseMethod;

      internal DC(ForeignWindow hwnd)
      {
         m_parent = hwnd;
         m_handle = Win32.GetWindowDC(hwnd.Handle);
         m_ReleaseMethod = ReleaseMethod.Release;
      }

      internal DC(DC compatible)
      {
         m_handle = Win32.CreateCompatibleDC(compatible.Handle);
         m_ReleaseMethod = ReleaseMethod.Delete;
      }

      internal IntPtr Handle
      {
         get
         {
            return m_handle;
         }
      }

      public Graphics Graphics
      {
         get
         {
            return Graphics.FromHdc(m_handle);
         }
      }

      public void CopyArea(Rectangle sourceRect, DC dest, Point destPoint)
      {
         Win32.BitBlt(dest.Handle, destPoint.X, destPoint.Y, sourceRect.Width, sourceRect.Height, m_handle, sourceRect.X, sourceRect.Y, 13369376);  // SRCCOPY
      }

      public void Dispose()
      {
         if (m_ReleaseMethod == ReleaseMethod.Release)
            Win32.ReleaseDC(m_parent.Handle, m_handle);
         else if (m_ReleaseMethod == ReleaseMethod.Delete)
            Win32.DeleteDC(m_handle);
      }


   }
}
